package com.xhb.ui.keys;

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.browser.callback.input.MoveMouseWheelCallback;
import com.teamdev.jxbrowser.browser.callback.input.PressKeyCallback;
import com.teamdev.jxbrowser.ui.KeyCode;
import com.tulskiy.keymaster.common.HotKey;
import com.tulskiy.keymaster.common.HotKeyListener;
import com.tulskiy.keymaster.common.Provider;
import com.xhb.EasyBrowserApplication;
import com.xhb.ui.Tab;
import com.xhb.ui.TabCaption;
import com.xhb.ui.TabbedPane;
import javax.swing.*;

/**
 * 快捷键处理
 */
public class ShortcutHandle {


    /**
     * 鼠标滚轮向上
     */
    private static final int UP = 1;

    private final Provider provider;

    /**
     * tab管理器
     */
    private final TabbedPane tabbedPane;

    private final JFrame frame;

    public ShortcutHandle(TabbedPane tabbedPane, JFrame frame) {
        this.tabbedPane = tabbedPane;
        this.frame = frame;
        provider = Provider.getCurrentProvider(true);
        provider.reset();
    }

    /**
     * 注册快捷键
     */
    public void regShortcutKeys() {
        regReload();
    }

    /**
     * 注册单个浏览器(browser)监听
     *
     * @param browser 浏览器对象
     */
    public void regBrowserShortcutKeys(Browser browser) {
        System.out.println(browser.zoom().level().toString());
        browser.set(MoveMouseWheelCallback.class, params -> {
            //按下了ctrl + 滚轮
            if (params.event().keyModifiers().isControlDown()) {
                if (params.event().deltaY() > 0) {
                    browser.zoom().in();
                } else {
                    browser.zoom().out();
                }
                return MoveMouseWheelCallback.Response.suppress();
            }
            return MoveMouseWheelCallback.Response.proceed();
        });
        browser.set(PressKeyCallback.class, params -> {
            //开发者模式
            if (params.event().keyCode() == KeyCode.KEY_CODE_F12) {
                SwingUtilities.invokeLater(() -> {
                    browser.devTools();
                });
            }
            return PressKeyCallback.Response.proceed();
        });
    }


    /**
     * 重新加载-F5
     */
    private void regReload() {
        provider.register(KeyStroke.getKeyStroke("F5"), new HotKeyListener() {
            @Override
            public void onHotKey(HotKey arg0) {
                SwingUtilities.invokeLater(() -> {
                    Browser browser = getNowSelectedBrowser();
                    if (browser != null) {
                        browser.navigation().reload();
                    }
                });
            }
        });
    }


    /**
     * 获取当前选中的browser对象
     *
     * @return
     */
    public static Browser getNowSelectedBrowser() {
        //获取当前tab
        TabCaption tabCaption = EasyBrowserApplication.tabbedPane.getSelectedTab();
        if (tabCaption != null) {
            Tab tab = EasyBrowserApplication.tabbedPane.findTab(tabCaption);
            if (tab != null) {
                return tab.getContent().getBrowserView().getBrowser();
            }
        }
        return null;
    }


}
